<!--
  * 字典 value 弹窗
  * 
  * @Author:    admin 
  * @Date:      2022-06-08 21:50:41 
    
   
  * @Copyright  jiaoyuyun.org.cn 
-->
<template>
  <a-drawer :width="800" :open="visible" :body-style="{ paddingBottom: '80px' }" title="地址" @close="onClose">
    <a-card size="small" :bordered="false">
      <!-- <a-row class="smart-table-btn-block">
        <div class="smart-table-operate-block">
          <a-button @click="addOrUpdateValue" type="primary" size="small">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button> -->
          <!-- <a-button @click="confirmBatchDelete" type="text" danger size="small" :disabled="selectedRowKeyList.length == 0">
            <template #icon>
              <DeleteOutlined />
            </template>
            批量删除
          </a-button> -->
        <!-- </div>
        <div class="smart-table-setting-block"></div>
      </a-row> -->
      <!-- <a-table
        size="small"
        :dataSource="tableData"
        :columns="columns"
        rowKey="dictValueId"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
        bordered
      >
        <template #bodyCell="{ record, column }">
          <template v-if="column.dataIndex === 'action'">
            <a-button @click="addOrUpdateValue(record)" type="link">编辑</a-button>
          </template>
        </template>
      </a-table> -->

    <a-table      
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="placeId"
      bordered
      :pagination="false"
      
      @expandedRowsChange="changeExand"
      :expanded-row-keys="expandedRowKeys"
    >
      <template #bodyCell="{ record, column }">
        <!-- <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="addCategory(record.placeId)" type="link" v-privilege="`${privilegePrefix}category:addChild`">增加子分类</a-button>
            <a-button @click="addCategory(undefined, record)" type="link" v-privilege="`${privilegePrefix}category:update`">编辑</a-button>
            <a-button @click="confirmDeleteCategory(record.placeId)" danger type="link"
              >删除</a-button
            >
          </div>
        </template> -->
      </template>
    </a-table>
      <!-- <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="ajaxQuery"
          @showSizeChange="ajaxQuery"
          :show-total="(total) => `共${total}条`"
        />
      </div> -->
    </a-card>
    <!-- <DictValueOperateModal ref="operateModal" @reloadList="ajaxQuery" /> -->
  </a-drawer>
</template>
<script setup>
  import { onMounted,reactive, ref } from 'vue';
  // import DictValueOperateModal from './school-modal.vue';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { placeApi } from '/@/api/business/place/place-api';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { Modal } from 'ant-design-vue';
  import { message } from 'ant-design-vue';
  import { smartSentry } from '/@/lib/smart-sentry';

  // 是否展示抽屉
  const visible = ref(false);
  const ID = ref(undefined);
  function changeExand(val) {
    expandedRowKeys.value = val;
  }
  function showModal(placeId) {
    ID.value = placeId;
    visible.value = true;
    ajaxQuery();
  }

  function onClose() {
    visible.value = false;
    ID.value = undefined;
  }

  const columns = reactive([
    {
      title: '地点名称',
      width: '50%',
      dataIndex: 'placeName',
    },
    {
      title: 'iP',
      dataIndex: 'valueName',
      align:"center"
    },
    // {
    //   title: '操作',
    //   dataIndex: 'action',
    //   width: 160,
    //   fixed: 'right',
    //   align:"center"
    // },
  ]);

  // ----------------------- 表格 查询 ------------------------

  const queryFormState = {
    placeId: '',
    placeName: '',
    parentId: '',
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });
  const selectedRowKeyList = ref([]);
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function onSelectChange(selectedRowKeys) {
    selectedRowKeyList.value = selectedRowKeys;
  }

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }
  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      queryForm.parentId = ID.value;
      let responseModel = await placeApi.tree(queryForm);
      // const list = responseModel.data.list;
      // total.value = responseModel.data.total;
      tableData.value = responseModel.data;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }
  const expandedRowKeys = ref([]);
  function reloadList(parentId) {
    ajaxQuery();
    if (parentId) {
      expandedRowKeys.value.push(parentId);
    }
  }
  // ----------------------- 批量 删除 ------------------------

  // function confirmBatchDelete() {
  //   Modal.confirm({
  //     title: '提示',
  //     content: '确定要删除选中值吗?',
  //     okText: '删除',
  //     okType: 'danger',
  //     onOk() {
  //       batchDelete();
  //     },
  //     cancelText: '取消',
  //     onCancel() {},
  //   });
  // }

  const batchDelete = async () => {
    try {
      SmartLoading.show();
      await dictApi.valueDelete(selectedRowKeyList.value);
      message.success('删除成功');
      ajaxQuery();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
    }
  };

  // ----------------------- 弹窗表单操作 ------------------------

  // const operateModal = ref();
  // function addOrUpdateValue(rowData) {
  //   operateModal.value.showModal(rowData, dictKeyId.value);
  // }

  onMounted(ajaxQuery);
  defineExpose({
    showModal,
  });
</script>
